<?php

namespace App\Console\Commands;

use App\Models\ModelNumberSpecification;
use App\Models\Product;
use App\Models\ProductCenter;
use App\Models\ProductJobType;
use App\Models\ProductModelNumber;
use Illuminate\Console\Command;

class UpProductCenter extends Command
{
    /**
     * The name and signature of the console command.
     * php artisan command:upproductcenter
     * @var string
     */
    protected $signature = 'command:upproductcenter';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $product = Product::where('is_del',2)->get();
        foreach($product as $k=>$v){
            $insert = [];
            $product_jobtype = ProductJobType::where('product_id',$v->id)->get();
            if($product_jobtype->count()>0){
                foreach($product_jobtype as $kk=>$vv){
                    //p($vv->job_type);
                    $model_numnber_arr = ProductModelNumber::where('product_id',$v->id)->pluck('model_number_id')->toArray();
                    foreach($model_numnber_arr as $kkk=>$vvv){
                        $specification_arr = ModelNumberSpecification::where('model_number_id',$vvv)->where('product_id',$v->id)->pluck('specification_id')->toArray();
                        foreach($specification_arr as $key=>$value){
                            $where = [
                                'product_id'=>$v->id,
                                'model_number_id'=>$vvv,
                                'specification_id'=>$value,
                                'job_type_id'=>$vv->job_type->id
                            ];
                            $res = ProductCenter::where($where)->first();
                            if(!$res){
                                $this->info('产品：'.$v->product_name.';型号：'.$vvv.';规格：'.$value.';工种：'.$vv->job_type->job_type);
                                $insert[] = $where;
                            }
                        }
                    }
                }
            }
            $this->info('产品：'.$v->product_name.'批量添加中...');
            ProductCenter::insert($insert);
        }
        //p($insert);

    }
}
